// created on 19.07.2007 at 19:11
using System;
using System.Collections;
using System.Collections.Generic;
using Gtk;
using Designer;

public class ObjectEditTab : HBox, iEditTab
{
	int pos;
	Obj editing;
	CheckButton checkVisible;
    CheckButton checkPersistent;
    NumEntry entryDepth;
   	List<Event> events;
    Hashtable eventIters = new Hashtable();
    ListStore eventStore;
    ListStore actionStore;
    Menu eventAddPop;
    
	private Gtk.VBox vbox1;
    private Gtk.Fixed fixedObjectGeneral;
    private Gtk.Label labelObjectName;
    private ResNameEntry entryName;
    private Gtk.Frame frame1;
    private Gtk.Alignment GtkAlignment4;
    private Gtk.Fixed fixedObjectSprite;
    private Gtk.Label GtkLabel6;
    private Gtk.Fixed fixedObjectMisc;
    private Gtk.Label label2;
    private Gtk.Button button32;
    private Gtk.Frame frame4;
    private Gtk.Alignment GtkAlignment3;
    private Gtk.VBox vbox3;
    private Gtk.ScrolledWindow scrolledwindow2;
    private Gtk.TreeView eventTree;
    private Gtk.HButtonBox hbuttonbox2;
    private Gtk.Button addEventButton;
    private Gtk.Button remEventButton;
    private Gtk.Label GtkLabel7;
    private Gtk.Frame frame2;
    private Gtk.Alignment GtkAlignment1;
    private Gtk.ScrolledWindow scrolledwindow4;
    private Gtk.TreeView actionTree;
    private Gtk.Label GtkLabel10;
    private Gtk.Notebook actionTabs;
    
    public ObjectEditTab(Obj edit, EventType preselect)
    {
    	Generate(edit);
    	foreach(Event e in events)
    	{
    		if(preselect== e.type)
    		{
    			Console.WriteLine("Preselecting Event "+e.Name+" at "+events.IndexOf(e));
		    	TreeSelection sel = eventTree.Selection;
				sel.SelectPath(new TreePath(events.IndexOf(e).ToString()));
		    	SelectEvent(e);
    			break;
    		}
    	}
    	
    }
	public ObjectEditTab(Obj edit)
	{
		Generate(edit);
	}
	private void Generate(Obj edit)
	{
		editing = edit;
		events = ((Obj)editing.Clone()).events;
		// Container child editTabs.Gtk.Notebook+NotebookChild
        this.Name = "hbox1";
        // Container child hbox1.Gtk.Box+BoxChild
        this.vbox1 = new Gtk.VBox();
        this.vbox1.Name = "vbox1";
        // Container child vbox1.Gtk.Box+BoxChild
        this.fixedObjectGeneral = new Gtk.Fixed();
        this.fixedObjectGeneral.HeightRequest = 80;
        this.fixedObjectGeneral.Name = "fixedObjectGeneral";
        this.fixedObjectGeneral.HasWindow = false;
        // Container child fixedObjectGeneral.Gtk.Fixed+FixedChild
        this.labelObjectName = new Gtk.Label();
        this.labelObjectName.Name = "labelObjectName";
        this.labelObjectName.LabelProp = "Name";
        this.fixedObjectGeneral.Add(this.labelObjectName);
        Gtk.Fixed.FixedChild w9 = ((Gtk.Fixed.FixedChild)(this.fixedObjectGeneral[this.labelObjectName]));
        w9.X = 5;
        w9.Y = 11;
        // Container child fixedObjectGeneral.Gtk.Fixed+FixedChild
        this.checkVisible = new Gtk.CheckButton();
        this.checkVisible.CanFocus = true;
        this.checkVisible.Name = "checkVisible";
        this.checkVisible.Label = "Sichtbar";
        checkVisible.Active = (bool)editing.Settings["visible"];
        this.checkVisible.DrawIndicator = true;
        this.checkVisible.UseUnderline = true;
        this.fixedObjectGeneral.Add(this.checkVisible);
        Gtk.Fixed.FixedChild w10 = ((Gtk.Fixed.FixedChild)(this.fixedObjectGeneral[this.checkVisible]));
        w10.X = 5;
        w10.Y = 45;
        // Container child fixedObjectGeneral.Gtk.Fixed+FixedChild
        this.checkPersistent = new Gtk.CheckButton();
        this.checkPersistent.CanFocus = true;
        this.checkPersistent.Name = "checkPersistent";
        this.checkPersistent.Label = "Bleibend";
        checkPersistent.Active = (bool)editing.Settings["persistent"];
        this.checkPersistent.DrawIndicator = true;
        this.checkPersistent.UseUnderline = true;
        this.fixedObjectGeneral.Add(this.checkPersistent);
        Gtk.Fixed.FixedChild w11 = ((Gtk.Fixed.FixedChild)(this.fixedObjectGeneral[this.checkPersistent]));
        w11.X = 85;
        w11.Y = 45;
        // Container child fixedObjectGeneral.Gtk.Fixed+FixedChild
        this.entryName = new ResNameEntry();
        this.entryName.WidthRequest = 120;
        this.entryName.CanFocus = true;
        this.entryName.Name = "entryName";
        entryName.ValidText = editing.Name;
        this.entryName.IsEditable = true;
        this.entryName.InvisibleChar = '•';
        this.fixedObjectGeneral.Add(this.entryName);
        Gtk.Fixed.FixedChild w12 = ((Gtk.Fixed.FixedChild)(this.fixedObjectGeneral[this.entryName]));
        w12.X = 50;
        w12.Y = 7;
        this.vbox1.Add(this.fixedObjectGeneral);
        Gtk.Box.BoxChild w13 = ((Gtk.Box.BoxChild)(this.vbox1[this.fixedObjectGeneral]));
        w13.Position = 0;
        w13.Expand = false;
        w13.Fill = false;
        // Container child vbox1.Gtk.Box+BoxChild
        this.frame1 = new Gtk.Frame();
        this.frame1.Name = "frame1";
        this.frame1.ShadowType = ((Gtk.ShadowType)(0));
        this.frame1.LabelXalign = 0F;
        // Container child frame1.Gtk.Container+ContainerChild
        this.GtkAlignment4 = new Gtk.Alignment(0F, 0F, 1F, 1F);
        this.GtkAlignment4.Name = "GtkAlignment4";
        this.GtkAlignment4.LeftPadding = ((uint)(12));
        // Container child GtkAlignment4.Gtk.Container+ContainerChild
        this.fixedObjectSprite = new Gtk.Fixed();
        this.fixedObjectSprite.HeightRequest = 150;
        this.fixedObjectSprite.Name = "fixedObjectSprite";
        this.fixedObjectSprite.HasWindow = false;
        this.GtkAlignment4.Add(this.fixedObjectSprite);
        this.frame1.Add(this.GtkAlignment4);
        this.GtkLabel6 = new Gtk.Label();
        this.GtkLabel6.Events = ((Gdk.EventMask)(256));
        this.GtkLabel6.Name = "GtkLabel6";
        this.GtkLabel6.LabelProp = "Grafik";
        this.GtkLabel6.UseMarkup = true;
        this.frame1.LabelWidget = this.GtkLabel6;
        this.vbox1.Add(this.frame1);
        Gtk.Box.BoxChild w16 = ((Gtk.Box.BoxChild)(this.vbox1[this.frame1]));
        w16.Position = 1;
        w16.Expand = false;
        w16.Fill = false;
        // Container child vbox1.Gtk.Box+BoxChild
        this.fixedObjectMisc = new Gtk.Fixed();
        this.fixedObjectMisc.HeightRequest = 150;
        this.fixedObjectMisc.Name = "fixedObjectMisc";
        this.fixedObjectMisc.HasWindow = false;
        // Container child fixedObjectMisc.Gtk.Fixed+FixedChild
        this.label2 = new Gtk.Label();
        this.label2.Name = "label2";
        this.label2.LabelProp = "Tiefe";
        this.fixedObjectMisc.Add(this.label2);
        Gtk.Fixed.FixedChild w17 = ((Gtk.Fixed.FixedChild)(this.fixedObjectMisc[this.label2]));
        w17.X = 5;
        w17.Y = 5;
        // Container child fixedObjectMisc.Gtk.Fixed+FixedChild
        this.entryDepth = new NumEntry();
        this.entryDepth.WidthRequest = 120;
        this.entryDepth.CanFocus = true;
        this.entryDepth.Name = "entryDepth";
        this.entryDepth.IsEditable = true;
        this.entryDepth.InvisibleChar = '•';
        entryDepth.Num = (int)editing.Settings["depth"];
        this.fixedObjectMisc.Add(this.entryDepth);
        Gtk.Fixed.FixedChild w18 = ((Gtk.Fixed.FixedChild)(this.fixedObjectMisc[this.entryDepth]));
        w18.X = 50;
        w18.Y = 1;
        // Container child fixedObjectMisc.Gtk.Fixed+FixedChild
        this.button32 = new Gtk.Button();
        this.button32.CanFocus = true;
        this.button32.Name = "button32";
        this.button32.UseStock = true;
        this.button32.UseUnderline = true;
        this.button32.Label = "gtk-ok";
        this.fixedObjectMisc.Add(this.button32);
        Gtk.Fixed.FixedChild w19 = ((Gtk.Fixed.FixedChild)(this.fixedObjectMisc[this.button32]));
        w19.X = 58;
        w19.Y = 115;
        this.vbox1.Add(this.fixedObjectMisc);
        Gtk.Box.BoxChild w20 = ((Gtk.Box.BoxChild)(this.vbox1[this.fixedObjectMisc]));
        w20.Position = 2;
        w20.Expand = false;
        w20.Fill = false;
        this.Add(this.vbox1);
        Gtk.Box.BoxChild w21 = ((Gtk.Box.BoxChild)(this[this.vbox1]));
        w21.Position = 0;
        w21.Expand = false;
        w21.Fill = false;
        // Container child hbox1.Gtk.Box+BoxChild
        this.frame4 = new Gtk.Frame();
        this.frame4.Name = "frame4";
        this.frame4.ShadowType = ((Gtk.ShadowType)(0));
        this.frame4.LabelXalign = 0F;
//        // Container child frame4.Gtk.Container+ContainerChild
        this.GtkAlignment3 = new Gtk.Alignment(0F, 0F, 1F, 1F);
        this.GtkAlignment3.Name = "GtkAlignment3";
        this.GtkAlignment3.LeftPadding = ((uint)(12));
        // Container child GtkAlignment3.Gtk.Container+ContainerChild
        this.vbox3 = new Gtk.VBox();
        this.vbox3.Name = "vbox3";
        // Container child vbox3.Gtk.Box+BoxChild
        this.scrolledwindow2 = new Gtk.ScrolledWindow();
        this.scrolledwindow2.CanFocus = true;
        this.scrolledwindow2.Name = "scrolledwindow2";
        this.scrolledwindow2.VscrollbarPolicy = ((Gtk.PolicyType)(1));
        this.scrolledwindow2.HscrollbarPolicy = ((Gtk.PolicyType)(1));
        // Container child scrolledwindow2.Gtk.Container+ContainerChild
        this.eventTree = new Gtk.TreeView();
        this.eventTree.CanFocus = true;
        this.eventTree.Name = "eventTree";
        this.scrolledwindow2.Add(this.eventTree);
        this.vbox3.Add(this.scrolledwindow2);
        Gtk.Box.BoxChild w23 = ((Gtk.Box.BoxChild)(this.vbox3[this.scrolledwindow2]));
        w23.Position = 0;
        // Container child vbox3.Gtk.Box+BoxChild
        this.hbuttonbox2 = new Gtk.HButtonBox();
        this.hbuttonbox2.WidthRequest = 0;
        this.hbuttonbox2.Name = "hbuttonbox2";
        this.hbuttonbox2.LayoutStyle = ((Gtk.ButtonBoxStyle)(3));
        // Container child hbuttonbox2.Gtk.ButtonBox+ButtonBoxChild
        this.addEventButton = new Gtk.Button();
        this.addEventButton.CanFocus = true;
        this.addEventButton.Name = "addEventButton";
        // Container child addEventButton.Gtk.Container+ContainerChild
        Gtk.Alignment w24 = new Gtk.Alignment(0.5F, 0.5F, 0F, 0F);
        w24.Name = "GtkAlignment2";
        // Container child GtkAlignment2.Gtk.Container+ContainerChild
        Gtk.HBox w25 = new Gtk.HBox();
        w25.Name = "GtkHBox1";
        w25.Spacing = 2;
        // Container child GtkHBox1.Gtk.Container+ContainerChild
        Gtk.Image w26 = new Gtk.Image();
        w26.Name = "image50";
        w26.Pixbuf = Gtk.IconTheme.Default.LoadIcon("gtk-add", 16, 0);
        w25.Add(w26);
        // Container child GtkHBox1.Gtk.Container+ContainerChild
        Gtk.Label w28 = new Gtk.Label();
        w28.Name = "GtkLabel4";
        w28.LabelProp = "";
        w25.Add(w28);
        w24.Add(w25);
        this.addEventButton.Add(w24);
        this.hbuttonbox2.Add(this.addEventButton);
        Gtk.ButtonBox.ButtonBoxChild w32 = ((Gtk.ButtonBox.ButtonBoxChild)(this.hbuttonbox2[this.addEventButton]));
        w32.Expand = false;
        w32.Fill = false;
        // Container child hbuttonbox2.Gtk.ButtonBox+ButtonBoxChild
        this.remEventButton = new Gtk.Button();
        this.remEventButton.CanFocus = true;
        this.remEventButton.Name = "remEventButton";
        this.remEventButton.UseUnderline = true;
        // Container child remEventButton.Gtk.Container+ContainerChild
        Gtk.Alignment w33 = new Gtk.Alignment(0.5F, 0.5F, 0F, 0F);
        w33.Name = "GtkAlignment5";
        // Container child GtkAlignment5.Gtk.Container+ContainerChild
        Gtk.HBox w34 = new Gtk.HBox();
        w34.Name = "GtkHBox2";
        w34.Spacing = 2;
        // Container child GtkHBox2.Gtk.Container+ContainerChild
        Gtk.Image w35 = new Gtk.Image();
        w35.Name = "image51";
        w35.Pixbuf = Gtk.IconTheme.Default.LoadIcon("gtk-remove", 16, 0);
        w34.Add(w35);
        // Container child GtkHBox2.Gtk.Container+ContainerChild
        Gtk.Label w37 = new Gtk.Label();
        w37.Name = "GtkLabel5";
        w37.LabelProp = "";
        w34.Add(w37);
        w33.Add(w34);
        this.remEventButton.Add(w33);
        this.hbuttonbox2.Add(this.remEventButton);
        Gtk.ButtonBox.ButtonBoxChild w41 = ((Gtk.ButtonBox.ButtonBoxChild)(this.hbuttonbox2[this.remEventButton]));
        w41.Position = 1;
        w41.Expand = false;
        w41.Fill = false;
        this.vbox3.Add(this.hbuttonbox2);
        Gtk.Box.BoxChild w42 = ((Gtk.Box.BoxChild)(this.vbox3[this.hbuttonbox2]));
        w42.Position = 1;
        w42.Expand = false;
        w42.Fill = false;
        this.GtkAlignment3.Add(this.vbox3);
        this.frame4.Add(this.GtkAlignment3);
        this.GtkLabel7 = new Gtk.Label();
        this.GtkLabel7.Events = ((Gdk.EventMask)(256));
        this.GtkLabel7.Name = "GtkLabel7";
        this.GtkLabel7.LabelProp = "Ereignisse";
        this.GtkLabel7.UseMarkup = true;
        //this.frame4.LabelWidget = this.GtkLabel7;
        this.Add(this.frame4);
        Gtk.Box.BoxChild w45 = ((Gtk.Box.BoxChild)(this[this.frame4]));
        w45.Position = 1;
        w45.Expand = false;
        w45.Fill = false;
        // Container child hbox1.Gtk.Box+BoxChild
        this.frame2 = new Gtk.Frame();
        this.frame2.Name = "frame2";
        this.frame2.ShadowType = ((Gtk.ShadowType)(0));
        this.frame2.LabelXalign = 0F;
        // Container child frame2.Gtk.Container+ContainerChild
        this.GtkAlignment1 = new Gtk.Alignment(0F, 0F, 1F, 1F);
        this.GtkAlignment1.Name = "GtkAlignment1";
        this.GtkAlignment1.LeftPadding = ((uint)(12));
        // Container child GtkAlignment1.Gtk.Container+ContainerChild
        this.scrolledwindow4 = new Gtk.ScrolledWindow();
        this.scrolledwindow4.CanFocus = true;
        this.scrolledwindow4.Name = "scrolledwindow4";
        this.scrolledwindow4.VscrollbarPolicy = ((Gtk.PolicyType)(1));
        this.scrolledwindow4.HscrollbarPolicy = ((Gtk.PolicyType)(1));
        // Container child scrolledwindow4.Gtk.Container+ContainerChild
        this.actionTree = new Gtk.TreeView();
        this.actionTree.WidthRequest = 200;
        this.actionTree.CanFocus = true;
        this.actionTree.Name = "actionTree";
        this.scrolledwindow4.Add(this.actionTree);
        this.GtkAlignment1.Add(this.scrolledwindow4);
        this.frame2.Add(this.GtkAlignment1);
        //this.vbox3.Add(this.scrolledwindow4);
        this.GtkLabel10 = new Gtk.Label();
        this.GtkLabel10.Events = ((Gdk.EventMask)(256));
        this.GtkLabel10.Name = "GtkLabel10";
        this.GtkLabel10.LabelProp = "Aktionen";
        this.GtkLabel10.UseMarkup = true;
        //this.frame2.LabelWidget = this.GtkLabel10;
        this.Add(this.frame2);
        Gtk.Box.BoxChild w49 = ((Gtk.Box.BoxChild)(this[this.frame2]));
        w49.Position = 2;
        w49.Expand = false;
        w49.Fill = false;
        // Container child hbox1.Gtk.Box+BoxChild
        this.actionTabs = new Gtk.Notebook();
        this.actionTabs.CanFocus = true;
        this.actionTabs.Name = "actionTabs";
        this.actionTabs.TabPos = ((Gtk.PositionType)(1));
        
        this.Add(this.actionTabs);
        Gtk.Box.BoxChild w54 = ((Gtk.Box.BoxChild)(this[this.actionTabs]));
        w54.Position = 3;
        // Notebook tab
        CloseableTab ctab = new CloseableTab("Objekt: "+editing.Name);
        ctab.closeButton.Clicked += OnCloseActivated;
        pos = ResourceMgr.Instance.Win.editTabs.AppendPage(this, ctab);
        ResourceMgr.Instance.Win.editTabs.ShowAll();
        this.button32.Clicked += new System.EventHandler(this.OnOKClicked);
        
        TreeViewColumn eventColumn = new TreeViewColumn ();
		eventColumn.Title = "Ereignisse";
		CellRendererText eventNameCell = new CellRendererText ();
		eventColumn.PackStart (eventNameCell, true);
 
		eventStore = new ListStore (typeof(Event));
		foreach (Event ev in events) {
			eventIters.Add(ev, eventStore.AppendValues(ev));
		}
 		eventTree.Reorderable = true;
		eventTree.Model = eventStore;
		eventTree.AppendColumn(eventColumn);
		eventColumn.SetCellDataFunc (eventNameCell, new Gtk.TreeCellDataFunc (RenderEvent));
		eventStore.RowChanged += OnEventRowChanged;
		eventTree.ButtonReleaseEvent+= OnEventButtonRelease;
		
		TreeViewColumn actionColumn = new TreeViewColumn ();
		actionColumn.Title = "Aktionen";
		CellRendererText actionNameCell = new CellRendererText ();
		actionColumn.PackStart (actionNameCell, true);
		actionStore = new ListStore (typeof (Designer.Action));
		actionTree.Model = actionStore;
 		actionTree.AppendColumn (actionColumn);
 		actionColumn.SetCellDataFunc (actionNameCell, new Gtk.TreeCellDataFunc (RenderAction));
 		actionTree.Reorderable = true;
 		actionStore.RowChanged += OnActionRowChanged;
 		actionTree.DragDataReceived += OnDragDataReceived;
 		actionTree.EnableModelDragDest(new TargetEntry[] {new TargetEntry("STRING",0, 0), new TargetEntry("GTK_TREE_MODEL_ROW",0, 1)}, Gdk.DragAction.Move | Gdk.DragAction.Copy);
 		actionTree.EnableModelDragSource(Gdk.ModifierType.Button1Mask,new TargetEntry[] {new TargetEntry("GTK_TREE_MODEL_ROW",0, 1)}, Gdk.DragAction.Move);
 		actionTree.KeyPressEvent += OnActionTreeKeyPress;
 		actionTree.RowActivated += OnActionTreeRowActivated;
		//INITIALIZE EVENT-ADD POPUP MENU
		eventAddPop = new Menu();
		MenuItem createEvent = new MenuItem("Create");
		createEvent.Activated += addEventCreate;
		eventAddPop.Append(createEvent);
		MenuItem stepEvent = new MenuItem("Step");
		stepEvent.Activated += addEventStep;
		eventAddPop.Append(stepEvent);
		MenuItem drawEvent = new MenuItem("Draw");
		drawEvent.Activated += addEventDraw;
		eventAddPop.Append(drawEvent);
		
		addEventButton.Clicked += OnEventAddClicked;
		remEventButton.Clicked += OnEventRemoveClicked;
		
		foreach(ActionSet aset in ResourceMgr.Instance.actionSets)
		{
			aset.GetTab(this.actionTabs);
		}
	}
	Event curEvent = null;
	
	protected virtual void OnEventButtonRelease(object o, Gtk.ButtonReleaseEventArgs args)
	{
		Gdk.EventButton eventbutton = args.Event;
		if(eventbutton.Button != 1)	return;
		TreePath path = new TreePath();
		if(!eventTree.GetPathAtPos((int)eventbutton.X, (int)eventbutton.Y, out path))
			return;
		TreeIter iter;
		eventStore.GetIter(out iter, path);
		Event ev = (Event)eventStore.GetValue(iter, 0);
		SelectEvent(ev);		
	}
	public void SelectEvent(Event ev)
	{
		Console.WriteLine("Selected event "+ev.Name+", Actions:");
		curEvent = ev;
		actionStore.Clear();
		dontCheckMove = true;
		foreach(Designer.Action act in curEvent.actions)
		{
			actionStore.AppendValues(act);
			Console.WriteLine(act.Title);
		}
		dontCheckMove = false;
	}
	void UnselectEvent()
	{
		TreeSelection sel = eventTree.Selection;
		sel.UnselectAll();
		actionStore.Clear();
	}
	
	
	bool dontCheckMove = false;
	private void RenderEvent (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
	{
		Event ev = (Event) model.GetValue (iter, 0);
		(cell as CellRendererText).Text = ev.Name;
	}
	private void RenderAction (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
	{
		Designer.Action act = (Designer.Action) model.GetValue (iter, 0);
		(cell as CellRendererText).Text = act.Title;
	}
	protected virtual void OnEventRowChanged(object o, Gtk.RowChangedArgs args)
	{
		Event ev = (Event)eventStore.GetValue(args.Iter, 0);
		Console.WriteLine("Event "+ev.Name+" moved to "+args.Path.ToString());
		events.Remove(ev);
		int index = int.Parse(args.Path.ToString());
		if(index > events.Count)
			events.Add(ev);
		else
			events.Insert(int.Parse(args.Path.ToString()), ev);
	}
	protected virtual void OnActionRowChanged(object o, Gtk.RowChangedArgs args)
	{
		if(dontCheckMove) return;
		Designer.Action act = (Designer.Action)actionStore.GetValue(args.Iter, 0);
		Console.WriteLine("Action "+act.Title+" moved to "+args.Path.ToString());
		curEvent.actions.Remove(act);
		int index = int.Parse(args.Path.ToString());
		if(index > curEvent.actions.Count)
			curEvent.actions.Add(act);
		else
			curEvent.actions.Insert(index, act);
	}
	protected virtual void OnOKClicked(object sender, System.EventArgs e)
	{
		ResourceMgr.Instance.Win.CloseEditTab(editing, true);
	}
	protected void OnCloseActivated(object o, EventArgs args)
	{
		Gtk.MessageDialog message = new MessageDialog(ResourceMgr.Instance.Win, DialogFlags.Modal, MessageType.Question, ButtonsType.None, false, "");
		message.Text = "Sollen die Änderungen übernommen werden?";
		message.Title = "Änderungen übernehmen?";
		message.AddButton(Stock.Yes, ResponseType.Yes);
		message.AddButton(Stock.No, ResponseType.No);
		message.AddButton(Stock.Cancel, ResponseType.Cancel);
		ResponseType RetVal = (ResponseType)message.Run();
		message.Destroy();
		if(RetVal == ResponseType.Yes || RetVal == ResponseType.No)
		{
			ResourceMgr.Instance.Win.CloseEditTab(editing, (RetVal == ResponseType.Yes));
		}
	}
	protected virtual void OnEventAddClicked(object o, EventArgs args)
	{
		eventAddPop.Popup();
		eventAddPop.ShowAll();
	}
	protected virtual void OnEventRemoveClicked(object o, EventArgs args)
	{
		if(curEvent == null) return;
		TreeIter iter = (TreeIter)eventIters[curEvent];
		eventStore.Remove(ref iter);
		UnselectEvent();
		eventIters.Remove(curEvent);
		events.Remove(curEvent);
		Console.WriteLine("Event "+curEvent.Name+" removed from object "+editing.Name);
		curEvent = null;
	}
	
	protected virtual void addEventCreate(object o, EventArgs args)
	{
		addEvent(Designer.EventType.Create);
	}
	protected virtual void addEventStep(object o, EventArgs args)
	{
		addEvent(Designer.EventType.Step);
	}
	protected virtual void addEventDraw(object o, EventArgs args)
	{
		addEvent(Designer.EventType.Draw);
	}
	void addAction(string type, int x, int y)
	{
		if(curEvent == null) return;
		ActionSet aset = (ActionSet)ResourceMgr.Instance.actionSets[actionTabs.CurrentPage];
		Console.WriteLine("Adding Action "+type+" from Action-Set "+aset.name+" to Event "+curEvent.Name);
		Designer.Action act = new Designer.Action(type, Designer.Action.GetLocation(editing, curEvent),aset);
		if(act.Type == "Code")
		{
			act.Args.Add(new ArrayList());
			ArrayList list = (ArrayList) act.Args[0];
			list.Add("Code");
			list.Add(new Code("", act.Location));
		}
		curEvent.actions.Add(act);
		
		TreePath path = new TreePath();
		TreeIter iter = actionStore.AppendValues(act);;
		if(!actionTree.GetPathAtPos(x, y, out path))
			return;
			
		TreeIter insertIter;
		actionStore.GetIter(out insertIter, path);
		actionStore.MoveAfter(iter,insertIter); 

//		eventStore.GetIter(out iter, path);
//		Event ev = (Event)eventStore.GetValue(iter, 0);
		
	}
	void addEvent(Designer.EventType type)
	{
		Event ev = null;
		foreach(Event e in events)
		{
			if(e.type == type)
			{
			Console.WriteLine("Event "+e.Name+" already exists in Object "+editing.Name+", going to definition");
			ev = e;
			break;
			}
		}
		if(ev == null)
		{
			ev = new Event(type,editing);
			Console.WriteLine("Adding Event "+ev.Name+" to Object "+editing.Name);
			events.Add(ev);
			eventIters.Add(ev,eventStore.AppendValues(ev));
		}
		TreeIter iter = (TreeIter)eventIters[ev];
		TreeSelection sel = eventTree.Selection;
		sel.SelectIter(iter);
		SelectEvent(ev);
		
	}
	public void Close(bool save)
	{
		if(save)
		{
			ResourceMgr.Instance.ChangeResourceName(editing, entryName.ValidText);
			editing.events = events;
			editing.Settings["visible"] = checkVisible.Active;
			editing.Settings["persistent"] = checkPersistent.Active;
			editing.Settings["depth"] = entryDepth.Num;
			//set sub-iters for object in resource tree (TODO: not just events but also actions)
			TreeIter objiter = ResourceMgr.Instance.GetIterByResource(this.editing);
			while(ResourceMgr.Instance.store.IterNChildren(objiter) > 0)
			{
				TreeIter deliter;
				ResourceMgr.Instance.store.IterNthChild(out deliter,objiter,0);
				Console.WriteLine(ResourceMgr.Instance.store.GetPath(deliter).ToString());
				ResourceMgr.Instance.store.Remove(ref deliter);
			}
			foreach(Event e in events)
			{
				ResourceMgr.Instance.store.AppendValues(objiter, e.Name);
			}	
		}
		ResourceMgr.Instance.Win.editTabs.Remove(this);
	}
	public int GetPosition()
	{
		return pos;
	}
	public void SetPosition(int pos)
	{
		this.pos = pos;
	}
	
	protected virtual void OnDragDataReceived  (object o, Gtk.DragDataReceivedArgs args)
	{
		if(args.Context.Action == Gdk.DragAction.Copy)
		addAction(args.SelectionData.Text, args.X, args.Y);
	}
	
	protected virtual void OnActionTreeKeyPress(object o, Gtk.KeyPressEventArgs args)
	{
		//Delete action
		if(args.Event.Key == Gdk.Key.Delete)
		{
			TreeSelection sel = actionTree.Selection;
			TreeIter iter;
			sel.GetSelected(out iter);
			Designer.Action act;
			try
			{
			act = (Designer.Action)actionStore.GetValue(iter, 0);
			}catch(Exception){
			return;
			}
			Console.WriteLine("Deleting Action "+act.Title+" from Event "+curEvent.Name);
			actionStore.Remove(ref iter);
			curEvent.actions.Remove(act);
		}
	}
	protected void OnActionTreeRowActivated(object o, Gtk.RowActivatedArgs args)
	{
		TreeIter iter;
		actionStore.GetIter(out iter,args.Path);
		Designer.Action act = (Designer.Action)actionStore.GetValue(iter, 0);
		act.OpenEditWin();
	}
	public void Reload()
	{
		entryName.ValidText = editing.Name;
	}
}